IT452 Advanced Web and Internet Systems
Set 9: Web Services (Chapter 28, loosely) Web Services
• “any service available on the Web that has been designed for consumption by programs, independent of the technology being used” • Two primary camps – REST (sometimes just “HTTP”) • “Representational State Transfer” • Exchanging documents • Many HTTP actions
– SOAP • Exchanging messages, or RPC • Mainly HTTP POST REST
• Use all of HTTP for sensible document management and caching: – POST – make new document – HEAD – get doc metadata – GET – retrieve document (no state change) – PUT – update document – DELETE – delete document • Requests – all state info is in the URL • Response format?
• In practice – often GET for everything – Works in browser – But violates “no side effects” rule SOAP
• Originally “Simple Object Access Protocol” • Two views – 1. Exchanging messages – 2. Performing RPC • Request – Mostly POST (but need not be just HTTP!) – A complex XML document – What parameters/functions are legal??
• Response format: XML
REST Example 1
• Get the weather from wunderground.com
• http://www.wunderground.com/weather/api/d/documentation.html (Ex 1) Weather XML Data From: http://api.wunderground.com/api/XXX/conditions/forecast/q/21409.xml
This is a webpage.
You have a lot of content on the page, and want to localize it for the user.
One easy way is to provide the weather!
Let's paste in your local weather using the wunderground.com web service, ask for a zipcode, and then use XSLT to transform the result into some nice XHTML.
We'll paste the result below.
Type your zip code:
This is where the transformed XML in XHTML form will appear.
// Get the XSLT file var xslhttp = new XMLHttpRequest(); xslhttp.open("GET", xslFileName, false); xslhttp.send('');
// Get the XML input data var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", url, false); xmlhttp.send('');
// Transform the XML via the XSLT var processor = new XSLTProcessor(); processor.importStylesheet(xslhttp.responseXML); var newDocument = processor.transformToDocument(xmlhttp.responseXML);
// Replace part of original document with the new content var o = document.getElementById("planet"); var n = newDocument.getElementById("planet"); o.parentNode.replaceChild(n, o); } (Ex 1) wunderground.pl
#!/usr/bin/perl use CGI ":standard"; use strict;
# Need to have "!head" to avoid loading the head function from LWP::Simple. # - The above CGI module also has a head function... use LWP::Simple "!head"; use LWP::UserAgent; use HTTP::Request; use HTTP::Response;
# We want to send XML back print "Content-type: text/xml\n\n";
# Construct URL to get the weather my $zip = param("zipcode"); my $URL = "http://api.wunderground.com/api/XXXX/conditions/forecast/q/$zip.xml";
# Get the XML document and send it back to requestor (the browser) my $contents = get($URL); print $contents; (Ex 1) wunderground.xsl Current weather in:
• Search flickr.com and show photos on your page. (Ex 2) Sample SOAP request
Online API: http://www.flickr.com/services/api/flickr.photos.search.html (Ex 2) Photos XML Data
(Ex 2) photos.html
Lets now use SOAP to access photos from flickr!
Tags to search for:
This is where the transformed XML in XHTML form will appear.
(Ex 2) flickr.pl (part 1) #!/usr/bin/perl use CGI ":standard";
# Need this to get web pages from Perl use LWP::Simple "!head"; use HTTP::Request; use LWP::UserAgent;
# We want to send XML back print "Content-type: text/xml\n\n"; my $ua = LWP::UserAgent->new(); my $method = "POST"; my $url = "http://api.flickr.com/services/soap/"; my $tags = param("tags"); my $content = "
print $the_response; } else { print $response->error_as_HTML; } (Ex 2) flickr.xsl
There were
// Get the XML input data var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", url, false); xmlhttp.send('');
// Transform the XML via the XSLT var processor = new XSLTProcessor(); processor.importStylesheet(xslhttp.responseXML); var newDocument = processor.transformToDocument(xmlhttp.responseXML);
// Replace part of original document with the new content var o = document.getElementById("planet"); var n = newDocument.getElementById("planet"); o.parentNode.replaceChild(n, o); }